package com.banciyuan.bcywebview.biz.detail.noveldetail.newnoveldetail;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.detail.complex.holder.ChargeHolder;
import com.banciyuan.bcywebview.biz.detail.complex.holder.CommentBottomHolder;
import com.banciyuan.bcywebview.biz.detail.complex.holder.CommentHolder;
import com.banciyuan.bcywebview.biz.detail.complex.holder.HeaderHolder;
import com.banciyuan.bcywebview.biz.detail.complex.holder.WorkAcgHolder;
import com.banciyuan.bcywebview.biz.detail.noveldetail.holder.ContentHolder;
import com.banciyuan.bcywebview.biz.detail.noveldetail.holder.IndexHolder;
import com.banciyuan.bcywebview.biz.detail.noveldetail.holder.TitleHolder;
import com.banciyuan.bcywebview.biz.detail.noveldetail.holder.ToolsHolder;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.Complex;
import de.greenrobot.daoexample.model.DetailComment;
import de.greenrobot.daoexample.model.DetailType;

/**
 * Created by squall on 2017/3/2.
 */

public class NovelComplexAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private Complex mItem;
    private List<DetailComment> dlist = new ArrayList<>();
    private DetailType detailType;

    private final static int HEADER = 0;
    private final static int TITLE = 1;
    private final static int INTRO = 2;
    private final static int CONTENT = 3;
    private final static int TOOLS = 4;
    private final static int CHARGE = 5;
    private final static int WORK = 6;
    private final static int COMMENT = 7;
    private final static int COMMENT_BOTTOM = 8;

    private LayoutInflater inflater;

    public NovelComplexAdapter(Context mContext, Complex mItem, DetailType detailType, List<DetailComment> comments) {
        this.mItem = mItem;
        this.dlist = comments;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
        this.detailType = detailType;
        this.mItem.setTimelineType(detailType.getTimelineType());
    }

    @Override
    public int getItemCount() {
        if (mItem.getStatus() == 1) {
            if (mItem.getLock() != null) {
                return 1;
            } else {
                return dlist.size() + 8;
            }
        } else {
            return 1;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEADER;
        } else if (position == 1) {
            return INTRO;
        } else if (position == 2) {
            return TITLE;
        } else if (position == 3) {
            return CONTENT;
        } else if (position == 4) {
            return TOOLS;
        } else if (position == 5) {
            return CHARGE;
        } else if (position == 6) {
            return WORK;
        } else if (position == getItemCount() - 1) {
            return COMMENT_BOTTOM;
        } else {
            return COMMENT;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case HEADER:
                return new HeaderHolder(inflater.inflate(R.layout.detail_firstheader, null));
            case INTRO:
                return new IndexHolder(inflater.inflate(R.layout.write_index_layout, null));
            case TITLE:
                return new TitleHolder(inflater.inflate(R.layout.detail_writer_header_layout, null));
            case CONTENT:
                return new ContentHolder(inflater.inflate(R.layout.novel_complex_content_item, null));
            case TOOLS:
                return new ToolsHolder(inflater.inflate(R.layout.novel_toolbar, null));
            case CHARGE:
                return new ChargeHolder(inflater.inflate(R.layout.charge_detail_item, parent,false));
            case WORK:
                return new WorkAcgHolder(inflater.inflate(R.layout.comment_line_layout, null), mContext, detailType.getTimelineType());
            case COMMENT_BOTTOM:
                return new CommentBottomHolder(inflater.inflate(R.layout.comment_bottom_layout, null));
            default:
                return new CommentHolder(inflater.inflate(R.layout.comment_detail_item, null), mItem.getUid());
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case HEADER:
                HeaderHolder headerHolder = (HeaderHolder) holder;
                headerHolder.setData(mItem, mContext);
                break;
            case TITLE:
                TitleHolder titleHolder = (TitleHolder) holder;
                titleHolder.setData(mContext, mItem);
                break;
            case INTRO:
                IndexHolder indexHolder = (IndexHolder) holder;
                indexHolder.setData(mContext, mItem);
                break;
            case CONTENT:
                ContentHolder contentHolder = (ContentHolder) holder;
                contentHolder.setData(mContext, mItem);
                break;
            case TOOLS:
                ToolsHolder toolsHolder = (ToolsHolder) holder;
                toolsHolder.setData(mContext, mItem);
                break;
            case CHARGE:
                ChargeHolder chargeHolder = (ChargeHolder) holder;
                chargeHolder.setData(mItem, mContext);
                break;
            case WORK:
                WorkAcgHolder workHolder = (WorkAcgHolder) holder;
                workHolder.setData(mItem, detailType.getTimelineType());
                break;
            case COMMENT_BOTTOM:
                CommentBottomHolder bottom = (CommentBottomHolder) holder;
                bottom.setData(mItem);
                break;
            default:
                int index = position - 7;
                CommentHolder commentHolder = (CommentHolder) holder;
                commentHolder.setData(index, dlist.get(index), mContext, true, detailType, false);
                break;
        }
    }

    public int getChargeIndex() {
        return 5;
    }

    public int getCommentStartIndex() {
        return 6;
    }

    public int getCommentSize() {
        return dlist.size();
    }

    public int getCommentIndex() {
        return 7;
    }


}
